package com.war.service.warehousing;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.warehousing.PurchaseDao;
import com.war.dao.warehousing.ReturnDao;
import com.war.pojo.warehousing.PurchaseInfo;
import com.war.pojo.warehousing.ReturnInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ReturnServiceImpl implements ReturnService {
    @Autowired(required = false)
    ReturnDao returnDao;

    @Autowired(required = false)
    PurchaseDao purchaseDao;

    @Override
    public HashMap<String, Object> selectReturnAll(ReturnInfo returnInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();//设置分页参数
        PageHelper.startPage(returnInfo.getPage(),returnInfo.getRow());
        List<ReturnInfo> returnList = new ArrayList<>();

        //查询数据库表数据
        //根据用户选择的查询条件进行查询
        //判断用户输入的查询条件是否有值
        if(returnInfo.getConValue().equals("") && !returnInfo.getCondition().equals("退货日期范围查询")){
            returnList = returnDao.selectAll(returnInfo);
        }else{
            if(returnInfo.getCondition().equals("产品名")){
                returnList = returnDao.selectByProductName(returnInfo);
            }else if(returnInfo.getCondition().equals("供应商")){
                returnList=returnDao.selectBySupplierName(returnInfo);
            }else if(returnInfo.getCondition().equals("采购员")){
                returnList=returnDao.selectByBuyer(returnInfo);
            }else if(returnInfo.getCondition().equals("退货日期范围查询")){
                returnList=returnDao.selectByDate(returnInfo);
            }else{
                returnList=returnDao.selectAll(returnInfo);
            }
        }

        //把查询的数据转换成分页对象
        PageInfo<ReturnInfo> page = new PageInfo<ReturnInfo>(returnList);

        //获取分页的当前页的集合
        map.put("returnList",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页码
        map.put("totalPage",page.getPages());
        //上一页
        //判断上一页的值是否等于第一页
        if(page.getPrePage() == 0)
        {
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }
        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());

        return map;
    }

    @Override
    public ReturnInfo selectById(ReturnInfo returnInfo) {
        return returnDao.selectById(returnInfo);
    }

    @Override
    public String editReturn(ReturnInfo returnInfo) {
        int num = returnDao.editReturn(returnInfo);
        if(num > 0){
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    public String delReturn(ReturnInfo returnInfo) {
        int num = returnDao.delReturn(returnInfo);
        if(num > 0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String insertReturn(ReturnInfo returnInfo) {
        int num = returnDao.insertReturn(returnInfo);
        if(num > 0){
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    public String ifExistWarehousingId(PurchaseInfo purchaseInfo) {
        PurchaseInfo purInfo = purchaseDao.selectById(purchaseInfo);
        if(purInfo == null){
            return "入库编号不存在";
        }
        return "入库编号存在";
    }

    @Override
    public int storageNumOver(ReturnInfo returnInfo) {
        return returnDao.storageNumNow(returnInfo);
    }

    @Override
    public String delAll(List<ReturnInfo> returnInfo) {
        int num = 0;
        for(ReturnInfo r:returnInfo){
            num = returnDao.delAll(r);
            num++;
        }
        if(num == returnInfo.size()){
            return "删除成功";
        }
        return "删除失败";
    }
}
